#include "pointitem.h"

#include <QGraphicsSceneMouseEvent>
#include <iostream>
#include <QPaintEngine>
#include "geometrydata.h"
#include "config.h"
#include <QRgb>
#include <QGraphicsScene>
#include <QGraphicsView>


PointItem::PointItem(Point *point)
{
    this->point = point;
    select_ = true;

    //updateParams();
}

void PointItem::updateParams()
{
    double sz = 10;
    double a = sz/this->scene()->views()[0]->transform().m11();
    rect = QRectF(point->x-1*a,point->y-1*a,2*a,2*a);
    pen = QPen(Config::meshColor());
    brush_empty = QBrush(Qt::NoBrush);
    double color_h = 0;
    double color_length = 0;

    color_length = abs(0 - 10);
    color_h = (360.0/color_length)*(point->psi);
    brush_colored = QBrush(QColor::fromHsvF(color_h/360.0,1,1));

    pen.setWidth(0);
}

QRectF PointItem::boundingRect() const
{
    return rect;
}

QPainterPath PointItem::shape() const
{
    QPainterPath path;
    path.addRect(rect);
    return path;
}

void PointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    updateParams();
    if (!select_)
        painter->setBrush(brush_empty);
    else
        painter->setBrush(brush_colored);

    if (Config::drawColoredMesh())
        painter->setBrush(brush_colored);

    if (!Config::drawMesh())
        pen = QPen(Qt::NoPen);
    else
        pen = QPen(Config::meshColor(),0);

    painter->setPen(pen);
    painter->drawRect(rect);

    if (Config::drawInfoText())
    {
        QPointF center( point->x,point->y);
        center = this->scene()->views()[0]->mapFromScene(center);
        painter->resetTransform();
        painter->setPen(QPen(Qt::black));
        painter->setFont(QFont("Arial",10));
        painter->drawText(center,QString::number(point->index));
    }

}

void PointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    update();
}

void PointItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseMoveEvent(event);
}

void PointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mouseReleaseEvent(event);
}
